<template>
  <div class="accountList">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="成分排行" name="first"> </el-tab-pane>
      <el-tab-pane label="功效宣称排行" name="third"> </el-tab-pane>
    </el-tabs>
    <div class="tableBox" ref="tableBoxRef">
      <el-table
      :height="640"
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        border
        :data="tableData"
        style="width: 100%"
      >
      
        <el-table-column type="index" align="center" label="排名" width="100">
        </el-table-column>
        <el-table-column
          v-if="activeName == 'first'"
          prop="content"
          align="center"
          label="成分"
        >
        </el-table-column>
        <el-table-column
          v-if="activeName == 'third'"
          prop="content"
          align="center"
          label="功效宣称"
        >
        </el-table-column>
        <el-table-column :formatter="formattedNumber"  align="center" label="点击次数">
         
        </el-table-column>
      </el-table>
        <el-card class="footer" v-if="total > 0">
         <div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      </el-card>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      activeName: "first",
      tableLoading: false,
      tableHeight: 50,
      tableData: [],
      pageNum: 1,
      pageSize: 15,
      total: 0,
    };
  },

  methods: {
    formattedNumber(row) {
    
        if (!row.tapCount) return '0';
        
      const num = parseFloat(row.tapCount);
      
      if (num >= 10000) {
        
        return (num / 10000).toFixed(1) + ' 万';
      }else {
    return num.toString();
  }

      },
    /**
     * 页码切换
     * @param {*} val
     */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
    
     
 
      let nameMap = {
        first: "ingredientList",
        third: "declareList",
      };
      this.getRawList(nameMap[this.activeName])
   
    },
    /**
     * 切换页面大小
     * @param {*} val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
        let nameMap = {
        first: "ingredientList",
        third: "declareList",
      };
      this.getRawList(nameMap[this.activeName])
    },
    handleClick(tab, event) {
      this.activeName = tab.name;
      this.pageNum = 1;
      this.pageSize = 15;
      let nameMap = {
        first: "ingredientList",
        third: "declareList",
      };
      this.getRawList(nameMap[tab.name])
      
    },
    async getRawList(url) {
      this.tableLoading = true;
      const res = await this.$api[url]({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      });
      this.total = res.data.result.total;
      this.tableData = res.data.result.list;
      this.tableLoading = false;
    },
  },
  created() {
    this.getRawList("ingredientList");
  },
};
</script>
<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
  .topBtn {
    margin: 10px 0;
    text-align: right;
  }
  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
</style>
